#include "BikeJosh.h"
#include <GL/glu.h>
#include "gllib/Objects.h"
#include <math.h>

void BikeJosh::draw() {
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

    float mat_specular[] = {0.7, 0.7, 0.7, 1.0};
    float shininess = 20;

    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialf(GL_FRONT, GL_SHININESS, shininess);

    glPushMatrix();
    glRotatef(90.0,1.0,0.0,0.0);

    //Roda Frontal
    drawTire();
    glColor3f(0.63, 0.74, 0.06);

    // LADO 1
    glPushMatrix();
    glTranslatef(0.0,0.05,0.0);
    glTranslatef(0.265,0.0,0.0);
    glRotatef(90,0.0,0.0,1.0);
    gllibCylinder(0.02,0.55,32);
    glTranslatef(0.0,-0.4,-0.4);
    gllibCylinder(0.02,0.55,32);
    glPopMatrix();
    //
    glPushMatrix();
    glTranslatef(0.0,0.05,0.0);
    glTranslatef(0.2,0.0,-0.2);
    glRotatef(90,0.0,0.0,1.0);
    glRotatef(45,1.0,0.0,0.0);
    gllibCylinder(0.02,0.55,32);

    glTranslatef(0.0,-0.37,0.37);
    gllibCylinder(0.02,0.55,32);
    glPopMatrix();

    //
    glPushMatrix();
    glTranslatef(0.0,0.05,0.0);
    glRotatef(90,1.0,0.0,0.0);
    glTranslatef(0.45,-0.3,0.0);
    glRotatef(-15,0.0,0.0,1.0);
    gllibCylinder(0.02,0.6,32);

    glTranslatef(0.5,0.15,0.0);
    gllibCylinder(0.02,0.6,32);
    glPopMatrix();

    // LADO 2
    glPushMatrix();
    glTranslatef(0.0,-0.05,0.0);
    glTranslatef(0.265,0.0,0.0);
    glRotatef(90,0.0,0.0,1.0);
    gllibCylinder(0.02,0.55,32);

    glTranslatef(0.0,-0.4,-0.4);
    gllibCylinder(0.02,0.55,32);
    glPopMatrix();
    //
    glPushMatrix();
    glTranslatef(0.0,-0.05,0.0);
    glTranslatef(0.2,0.0,-0.2);
    glRotatef(90,0.0,0.0,1.0);
    glRotatef(45,1.0,0.0,0.0);
    gllibCylinder(0.02,0.55,32);

    glTranslatef(0.0,-0.37,0.37);
    gllibCylinder(0.02,0.55,32);
    glPopMatrix();

    //
    glPushMatrix();
    glTranslatef(0.0,-0.05,0.0);
    glRotatef(90,1.0,0.0,0.0);
    glTranslatef(0.45,-0.3,0.0);
    glRotatef(-15,0.0,0.0,1.0);
    gllibCylinder(0.02,0.6,32);

    glTranslatef(0.5,0.15,0.0);
    gllibCylinder(0.02,0.6,32);
    glPopMatrix();


    //Roda Traseira
    glPushMatrix();
    glTranslatef(1.05,0.0,0.0);
    drawTire();
    glPopMatrix();

    //
    glColor3f(0.63, 0.74, 0.06);
    glPushMatrix();
    gllibCylinder(0.02,0.06,32);
    glTranslatef(1.05,0.0,0.0);
    gllibCylinder(0.02,0.06,32);
    glTranslatef(-0.13,0.0,-0.4);
    gllibCylinder(0.02,0.06,32);
    glTranslatef(-0.5,0.0,0.0);
    gllibCylinder(0.02,0.06,32);
    glTranslatef(0.1,0.0,0.4);
    gllibCylinder(0.02,0.06,32);
    glTranslatef(0.37,0.0,-0.58);
    glColor3f(0.85, 0.85, 0.85);
    gllibCylinder(0.02,0.5,32);
    glPopMatrix();

    //
    glPushMatrix();
    glTranslatef(0.3,0.0,-0.63);
    drawSit();
    glPopMatrix();

    //
    glPushMatrix();
    glTranslatef(0.55,0.07,0.0);
    drawGear();
    glTranslatef(0.0,0.0,0.07);
    glColor3f(1.0, 1.0, 1.0);

    glPushMatrix();
    glScalef(0.1,0.02,0.3);
    gllibCube(0.5);
    glPopMatrix();
    glPushMatrix();
    glScalef(0.1,0.15,0.02);
    glTranslatef(0.0,0.22,4.0);
    gllibCube(0.5);
    glPopMatrix();

    glPopMatrix();

    //
    glPushMatrix();
    glTranslatef(0.55,-0.07,0.0);
    drawGear();
    glTranslatef(0.0,0.0,-0.07);
    glColor3f(1.0, 1.0, 1.0);

    glPushMatrix();
    glScalef(0.1,0.02,0.3);
    gllibCube(0.5);
    glPopMatrix();
    glPushMatrix();
    glScalef(0.1,0.15,0.02);
    glTranslatef(0.0,-0.22,-4.0);
    gllibCube(0.5);
    glPopMatrix();

    glPopMatrix();


    glPopMatrix();

    glDisable(GL_COLOR_MATERIAL);
}

void BikeJosh::drawTire(){
    glColor3f(0.18, 0.18, 0.18);
    gllibTorus(0.025, 0.25, 32, 32);
    int maxDiv = 20;
    glColor3f(0.85, 0.85, 0.85);
    glBegin(GL_LINES);
        for(int i = 0; i < maxDiv ; i++){
            glVertex3f(0.0,0.0,0.0);
            glVertex3f(cos((2*M_PI /maxDiv)*i)*0.225,0.0,sin((2*M_PI /maxDiv)*i)*0.225);
        }
    glEnd();
}

void BikeJosh::drawSit(){
    glColor3f(0.18, 0.18, 0.18);
    glBegin(GL_POLYGON);
        glVertex3f(0.2,0.0,0.0);
        glVertex3f(0.0,-0.13,0.0);
        glVertex3f(0.0,0.13,0.0);
    glEnd();
    glBegin(GL_QUAD_STRIP);
        glVertex3f(0.2,0.0,0.05);
        glVertex3f(0.2,0.0,0.0);
        glVertex3f(0.0,0.13,0.05);
        glVertex3f(0.0,0.13,0.0);
        glVertex3f(0.0,-0.13,0.05);
        glVertex3f(0.0,-0.13,0.0);
        glVertex3f(0.2,0.0,0.05);
        glVertex3f(0.2,0.0,0.0);
    glEnd();
    glBegin(GL_POLYGON);
        glVertex3f(0.2,0.0,0.05);
        glVertex3f(0.0,0.13,0.05);
        glVertex3f(0.0,-0.13,0.05);
    glEnd();
}

void BikeJosh::drawGear(){
    glPushMatrix();
    glColor3f(0.18, 0.18, 0.18);
    glScalef(1.0,0.01,1.0);
    gllibSphere(0.08,10);
    glPopMatrix();
}

